<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="title" v-model="visible" align-center width="500px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="暂停时间" prop="pause_time" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-date-picker v-model="form.pause_time" type="daterange" value-format="YYYY-MM-DD" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期" :disabled-date="val => val.getTime() < Date.now()" />
      </el-form-item>
      <el-form-item label="暂停备注" prop="pause_reason">
        <el-input v-model="form.pause_reason" placeholder="账号类型" type="textarea"  :autosize="{ minRows: 2, maxRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick, reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const title = ref('暂停下单')

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (pdcs_ids) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  if (pdcs_ids.length > 1) {
    title.value = '批量暂停下单'
  }
  form.value = {
    pdcs_ids,
    pause_reason: '',
    pause_time: []
  }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  if (!await formRef.validate()) return
  loadings.create = true
  form.value.pause_start_time = form.value.pause_time[0]
  form.value.pause_end_time = form.value.pause_time[1]
  api.pause(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>